package xyz.scootaloo.oj.offer;

import java.util.Stack;

/**
 * 栈
 * 设计
 * @author flutterdash@qq.com
 * @since 2021/3/27 15:41
 */
public class P30 {

    private static class MinStack {
        private final Stack<Integer> data;
        private final Stack<Integer> minStack;

        /** initialize your data structure here. */
        public MinStack() {
            data = new Stack<>();
            minStack = new Stack<>();
        }

        public void push(int x) {
            if (data.isEmpty()) {
                data.push(x);
                minStack.push(x);
            } else {
                data.push(x);
                if (x < minStack.peek()) {
                    minStack.push(x);
                } else {
                    minStack.push(minStack.peek());
                }
            }
        }

        public void pop() {
            data.pop();
            minStack.pop();
        }

        public int top() {
            return data.peek();
        }

        public int min() {
            return minStack.peek();
        }

    }

    public static void main(String[] args) {
        MinStack stack = new MinStack();
        stack.push(-2);
        stack.push(0);
        stack.push(-3);
        System.out.println(stack.min());
        stack.pop();
        System.out.println(stack.top());
        System.out.println(stack.min());
    }

}
